home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / hips / sources / tools / hexdump.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-01  |  1.8 KB  |  82 lines

  1. /*    HexDUMP . C
  2. %
  3. %    Copyright (c)    Jin Guojun
  4. %
  5. %AUTHOR:    Jin Guojun - LBL    Oct. 30, 1990
  6. */
  7. #include <ctype.h>
  8. #include "stdef.h"
  9.  
  10. arg_fmt_list_string    arg_fmt[] =    {
  11.     {"-b[x]", "%i %x", 0, 1, 1, "begin at given position. x = HEX"},
  12.     {"-d[x]", "%i %x", 0, 1, 1,
  13.         "displament relocates the starting position"},
  14.     {"-l", "%-", 0, 1, 0, "tell file size only"},
  15.     {" in -- name or pipe.    out -- standard out.", No, 0, 0, 0,
  16.         "note:    This dump will skip repeating lines."}, NULL    };
  17. char*    Progname;
  18.  
  19. #define    LineSize    16
  20.  
  21. #if    defined sparc | defined    NEWisprint
  22. #undef    isprint
  23. #define    isprint(c)    ((c) > 0x1F && (c) < 0x7F)
  24. #endif
  25.  
  26.  
  27. main(argc, argv)
  28. int    argc;
  29. char **    argv;
  30. {
  31. int    l=0, lns=0, r=0, s=0, offset=0;
  32. char    buf[LineSize], bbuf[LineSize], **fl;
  33. FILE*    in_fp = stdin;
  34. register char*    p = buf, *bp = bbuf;
  35.  
  36. Progname = *argv;
  37. if (parse_argus(&fl, argc, argv, arg_fmt, &l, &l, &offset, &offset, &l) < 0)
  38.     exit(-1);
  39.  
  40. if ((in_fp = freopen(fl[0], "rb", stdin)) != stdin)    {
  41.     fprintf(stderr, "can't open %s to read\n", fl[0]);    exit(1);
  42.     }
  43.  
  44. if (l < 0)    {
  45.     fseek(in_fp, 0, 2);
  46.     l = ftell(in_fp);
  47.     fprintf(stderr, "file size is %d[%XH]\n", l, l);
  48.     exit(0);
  49. }
  50. if(l)    offset = l;
  51. if (offset)    fseek(in_fp, offset, 0);
  52. do {
  53. register int    i;
  54. loop:    l += s;    lns++;
  55.     s = fread(p, sizeof(char), LineSize, in_fp);
  56.     if (!s)    goto    prt;
  57.     for (i=0; i < s && p[i] == bp[i]; i++);
  58.     if (i == s)
  59.     {    r++;    goto    loop;    }
  60.     else if (r) {
  61. prt:        printf("** %02X ... skipped %d[%XH] lines\n", bp[0]&0xFF,r,r);
  62.         r = 0;
  63.         }
  64.  
  65.     for (i=0; i < s && i < 8; i++)    printf("%02X ", p[i] & 0xFF);
  66.     printf("- ");
  67.     while (i < s)    printf("%02X ", p[i++] & 0xFF);
  68.     printf("%2c", ' ');
  69.  
  70.     for (i=0; i < s; i++)
  71.         if (isprint(p[i]))
  72.             printf("%c", p[i]);
  73.         else    printf(".");
  74.     printf("  %08X\n", l);
  75.     memcpy(bp, p, s);
  76.     }    while(s == 16 && !feof(in_fp));
  77. fclose(in_fp);
  78. printf("Total %d lines<%d bytes>\n", lns, --lns*16+s);
  79.  
  80. exit(0);
  81. }
  82.